package org.finesys.common.disruptor.event.handler;

import lombok.extern.slf4j.Slf4j;
import org.finesys.common.disruptor.event.DisruptorEvent;
import org.finesys.common.disruptor.event.handler.chain.HandlerChain;


@Slf4j
public abstract class AbstractEnabledEventHandler<T extends DisruptorEvent> extends AbstractNameableEventHandler<T> {


    protected boolean enabled = true;

    protected abstract void doHandlerInternal(T event, HandlerChain<T> handlerChain) throws Exception;

    @Override
    public void doHandler(T event, HandlerChain<T> handlerChain) throws Exception {

        if (!isEnabled(event)) {
            log.debug("Handler '{}' is not enabled for the current event.  Proceeding without invoking this handler.",
                    getName());
            // Proceed without invoking this handler...
            handlerChain.doHandler(event);
        } else {
            log.trace("Handler '{}' enabled.  Executing now.", getName());
            doHandlerInternal(event, handlerChain);
        }

    }

    protected boolean isEnabled(T event) throws Exception {
        return isEnabled();
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

}
